package com.wangkang.eduservice.service.impl;
import com.wangkang.eduservice.entity.EduChapter;
import com.wangkang.eduservice.entity.EduVideo;
import com.wangkang.eduservice.entity.vo.EduChapterVo;
import com.wangkang.eduservice.entity.vo.EduVideoVo;
import com.wangkang.eduservice.enums.EduResultCode;
import com.wangkang.eduservice.mapper.EduChapterMapper;
import com.wangkang.eduservice.service.EduChapterService;
import com.wangkang.eduservice.service.EduVideoService;
import com.wangkang.servicebase.exceptionhandler.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wangkang
 * @since 2022-04-13
 */
//用this或baseMapper来调用底层已定义好的方法，但仅能用baseMapper调用自己定义的方法。
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;

    public EduChapterServiceImpl(EduVideoService videoService) {
        this.videoService = videoService;
    }

    /**
     * 根据课程ID 查询课程的章节小节
     *
     * @param courseId 课程id
     * @return 章节小节
     */
    @Override
    public List<EduChapterVo> getChapterVideoByCourseId(String courseId) {

       //1.根据课程id查询课程里面所有的章节
       QueryWrapper<EduChapter> wrapperChapter=new QueryWrapper<>();
       //根据chapter表中的course_id字段，作为查询条件，通过selectList方法去查询出EduChapter章节对象。
       wrapperChapter.eq("course_id",courseId);
       List<EduChapter> eduChapterList=baseMapper.selectList(wrapperChapter);


       //2.根据课程id查询课程里面所有的小节
       QueryWrapper<EduVideo>  wrapperVideo=new QueryWrapper<>();
       //根据video表中的course_id字段，作为查询条件，通过list方法去查询出EduVideo小节对象。
       wrapperVideo.eq("course_id",courseId);
       List<EduVideo> eduVideoList=videoService.list(wrapperVideo);

       //创建list集合,用于最终封装数据
       List<EduChapterVo>  finalList=new ArrayList<>();


       //3.遍历查询章节list集合进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {
         //得到每个章节
         EduChapter eduChapter=eduChapterList.get(i);
         //eduChapter对象复制到eduChapterVo里面
         EduChapterVo eduChapterVo=new EduChapterVo();
         BeanUtils.copyProperties(eduChapter,eduChapterVo);
         //把eduChapterVo放到最终list集合
         finalList.add(eduChapterVo);


        //创建list集合,用于最终封装章节的小节
        List<EduVideoVo>  videoList=new ArrayList<>();

       //4.遍历查询小节list集合进行封装
        for (int m = 0; m < eduVideoList.size(); m++) {
            //得到每个小节
            EduVideo eduVideo = eduVideoList.get(m);
            //判断：小节里面chapterId和章节里面id是否一样
            if (eduVideo.getChapterId().equals(eduChapterVo.getId())) {
             EduVideoVo eduVideoVo=new EduVideoVo();
             BeanUtils.copyProperties(eduVideo,eduVideoVo);
             videoList.add(eduVideoVo);
            }
         }
        //把封装之后的小节list集合,放到章节对象里面
        eduChapterVo.setChildren(videoList);

     }
   return finalList;



//        //1.根据课程id 查询课程里面所有的章节
//        LambdaQueryWrapper<EduChapter> chapterQuery = new LambdaQueryWrapper<>();
//        chapterQuery.eq(EduChapter::getCourseId,courseId);
//        List<EduChapter> chapterList = this.list(chapterQuery);
//
//        //封装章节
//        List<EduChapterVo> chapterVoList = chapterList.stream()
//                .map(chapter -> new EduChapterVo(chapter.getId(), chapter.getTitle()))
//                .collect(toList());
//
//
//        //2.根据课程id查询课程里面所有的小节,Chapter中不能直接查询Video,所以需要先注入Video
//        LambdaQueryWrapper<EduVideo> videoQuery = new LambdaQueryWrapper();
//        videoQuery.eq(EduVideo::getCourseId,courseId);
//        List<EduVideo> videoList = videoService.list(videoQuery);
//        //封装小节
//        chapterVoList.forEach(chapterVo -> {
//
//            List<EduVideoVo> videoVoList = videoList.stream()
//                    .filter(video -> video.getChapterId().equals(chapterVo.getId()))
//                    .map(video -> new EduVideoVo(video.getId(), video.getTitle(),video.getVideoSourceId()))
//                    .collect(toList());
//
//            chapterVo.setChildren(videoVoList);
//        });
//
//        return chapterVoList;
    }

    /**
     * 根据章节id删除章节
     *
     * @param chapterId
     */
    @Override
    public void deleteChapter(String chapterId) {
        //根据chapterId 查询小节表，如果查询有数据，不进行删除。
        LambdaQueryWrapper<EduVideo> chapterQuery = new LambdaQueryWrapper<>();
        //根据video表中的chapter_id字段，作为查询条件，通过count方法去查询小节。
        chapterQuery.eq(EduVideo::getChapterId,chapterId);
        int count = videoService.count(chapterQuery);
        if (count>0){
            throw GuliException.from(EduResultCode.DELETE_ERROR);
        }
        //删除章节
        boolean result = removeById(chapterId);
        if(!result){
            throw GuliException.from(EduResultCode.DELETE_ERROR);
        }
    }

    /**
     * 根据课程id 删除章节
     *
     * @param courseId 课程id
     */
    @Override
    public void deleteChapterByCourseId(String courseId) {
        LambdaQueryWrapper<EduChapter> chapterWrapper = new LambdaQueryWrapper<>();
        //根据chapter表中的course_id字段，作为查询条件，通过remove方法去删除章节。
        chapterWrapper.eq(EduChapter::getCourseId,courseId);
        //删除章节
        boolean removeResult = this.remove(chapterWrapper);
        if(!removeResult){
            throw  GuliException.from(EduResultCode.DELETE_ERROR);
        }
    }
}